package com.bwie.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.bwie.user.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.user.entity.UserEntity;
import com.bwie.user.service.UserService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author ÕÔË«Ò¶
 * @email 1535715783@qq.com
 * @date 2022-01-16 20:46:55
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        System.out.println("2222");

        return R.ok().put("page", page);
    }


    /**
     *登录
     */
    @RequestMapping("/info")
    public R login(@RequestBody UserEntity user){
        UserEntity user1 = userService.getById(user.getUphone());
        if (user1!=null){
            if (user.getPwd().equals(user1.getPwd())){
                String token = jwtUtil.createJWT(user1.getUphone(), user1.getUphone(), "admin");

                return R.ok().put("token",token).put("user",user1);
            }

            return R.error();
        }
        return R.error();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{uphone}")
    public R info(@PathVariable("uphone") String uphone){
		UserEntity user = userService.getById(uphone);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] uphones){
		userService.removeByIds(Arrays.asList(uphones));

        return R.ok();
    }

}
